﻿function changeBoard(BoardId)
{
   window.location= "/securitiesinfo/index?boardid=" + BoardId;
}

function changeBoard2(BoardId,uid)
{
   window.location= "/securitiesinfo/securitiesuser?boardid=" + BoardId + "&userid=" + uid;
}

function EditFocus(id)
{
    OpenDialogBox("/securitiesinfo/layer_editfocus?focusid=" + id,"修改自选股信息",450,function(){
        $("#idBoxConfirm").bind("click",function(){
            var cBoardId = $("#cBoardId").val();
            var cReason = $("#cReason").val();
            
            if(cReason.length > 15)
            {
                alert("备注超过了15个字符!");
                return;
            }
            
            $.post("/SecuritiesInfo/ajax_editfocus",{focusid:id,BoardId:cBoardId,Reason:cReason},function(data){
                if(data == "success")
                {
                    CloseBox();
                    window.location.reload();
                }
                else 
                {
                    CloseBox();
                    OpenAlertBoxByText("数据异常","提示",350,null);
                }
            },"TEXT");
        });
    });
}

function ViewFocus(id)
{
    OpenAlertBox("/securitiesinfo/layer_viewfocus?focusid=" + id,"查看自选股信息",450);
}

//显示更新标记价
function UpdateFocusPrice(id)
{
    $.getJSON("/securitiesinfo/ajax_showFocusPrice",{focusId:id,t:Math.random()},function(json){
        var html = '刷新选中将更新您选中股票的时间和价格<br><br>选中价格：'+json["FocusPrice"]+' 更新至 ';
        html += json["NowPrice"]+'<br>';
        
        OpenDialogBoxByText(html,"刷新选中",350,function(){
            $("#idBoxConfirm").bind("click",function(){CloseBox();
                $.post("/securitiesinfo/ajax_updateFocusPrice",{focusId:id,focusPrice:json["NowPrice"]},function(data){
                    if(data == "success")window.location.reload();
                    else
                    {
                        OpenAlertBoxByText("更新失败："+data,"提示",350,null);
                    }
                },"TEXT");
                });
        });
        
    });
}

//更新自选股是否监视
function UpdateFocusScout(id,scout)
{
    var action = (scout=="true"?"添加":"移除");

    $.post("/securitiesinfo/ajax_updateScout",{focusId:id,scout:scout},function(data){
        if(data == "success")
        {
            OpenAlertBoxByText(action+"成功","关注详情",350,function(){
                $("#idBoxClose").unbind("click");
                $("#idBoxConfirm").unbind("click");
            
                $("#idBoxClose").bind("click",function(){CloseBox();window.location.reload();});
                $("#idBoxConfirm").bind("click",function(){CloseBox();window.location.reload();});
            });
        }
        else OpenAlertBoxByText(action+"失败："+data,"提示",350,null);
    },"TEXT");
}

//显示移除自选股的监视
function showDeleteFocusScout(id,scout,name)
{
    var html = '<span style="color:#999">移除监视后该自选股将不在监视器中出现，但仍在您的自选股列表中。</span><br><br>您确定要移除<span style="color:blue"> '+name+' </span>的监视吗？';
    html += '<br /><br /><input type="checkbox"  id="deletefocus"  /> 同时在自选股中删除';
    
    OpenDialogBoxByText(html,"移除监视",350,function(){

        $("#idBoxConfirm").bind("click",function(){
            var isdelete = $("#deletefocus").attr("checked");
            CloseBox();
            if(isdelete)
            {
                $.post("/SecuritiesInfo/ajax_cancelFocus",{id:id+"|"},function(data){
                    if(data == "success")
                    {
                        OpenAlertBoxByText("删除成功","提示",350,function(){
                            $("#idBoxClose").unbind("click");
                            $("#idBoxConfirm").unbind("click");
                        
                            $("#idBoxClose").bind("click",function(){CloseBox();window.location.reload();});
		                    $("#idBoxConfirm").bind("click",function(){CloseBox();window.location.reload();});
                        });
                    }
                    else OpenAlertBoxByText("删除失败","数据错误",350,null);
                },"TEXT");
            }
            else
            {
                UpdateFocusScout(id,scout);
            }
        });
    
    });
    
}

function AllCheck()
{
    $("input[type='checkbox']").attr("checked",true);
}

function CancelCheck()
{
    var checkbox = $("input[type='checkbox'][checked]");
    
    var id = "";
    
    for(var i = 0; i < checkbox.length;i++)
    {
        id += checkbox[i].id + "|";
    }
    
    if(id != "")
    {
        var html = '<div>您确定要吧这些证券从自选股删除么</div>';

        OpenDialogBoxByText(html, "删除自选股", 350, 
            function() {
            $("#idBoxConfirm").bind("click",function(){CloseBox();
                $.post("/SecuritiesInfo/ajax_cancelFocus",{id:id},function(data){
                if(data == "success")
                {
                    OpenAlertBoxByText("删除成功","提示",350,function(){
                        $("#idBoxClose").unbind("click");
                        $("#idBoxConfirm").unbind("click");
                    
                        $("#idBoxClose").bind("click",function(){CloseBox();window.location.reload();});
		                $("#idBoxConfirm").bind("click",function(){CloseBox();window.location.reload();});
                    });
                }
                else OpenAlertBoxByText("删除失败","数据错误",350,null);
            },"TEXT");
            
            });
        });
    }
    else OpenAlertBoxByText("请先选择要删除的证券","提示",350,null);
}

function changeFriendConcern(type)
{
    if (type == 1) window.location = "/securitiesinfo/friendconcernfocus";
    else window.location = "/securitiesinfo/friendconcernfocus?action=concern";
}

function AddNewBoard()
{
    var name = $("#Name").val();
    var desc = $("#Desc").val();
    
    if(name == "" || desc == "" || name == "输入名称" || desc == "输入描述")
    {
        alert("请填写板块名称和描述");
        return;
    }
    else
    {
        if(name.length > 15 || desc.length > 50)
        {
           alert("板块名称在15个字符内,描述在50个字符内");
           return;
        }
    
        $.post("/SecuritiesInfo/ajax_AddNewBoard",{Name:name,Desc:desc},function(data){
            
            if(data == "success")
            {
                window.location.reload();
            }
            else OpenAlertBoxByText("添加失败","数据错误",350,null);
        },"TEXT");
    }
}

function EditBoard(BoardId)
{
    OpenDialogBox("/securitiesinfo/layer_editboard?boardid=" + BoardId,"修改板块信息",500,function(data){
         $("#idBoxConfirm").bind("click",function(){
            
            var cName = $("#CName").val();
            var cDesc = $("#CDesc").val();
            
            if(cName == "" || cDesc == "")
            {
                alert("请填写板块名称和描述");
                return;
            }
            
            if(cName.length > 15 || cDesc.length > 50)
            {
                alert("板块名称在15个字符内,描述在50个字符内");
                return;
            }
            
            $.post("/SecuritiesInfo/ajax_EditBoard/",{BoardId:BoardId,Name:cName,Desc:cDesc},function(data){
                
                if(data == "success")
                {
                    CloseBox();
                    window.location.reload();
                }
                else 
                {   
                    CloseBox();
                    OpenAlertBoxByText("修改失败","数据错误",350,null);
                }
            },"TEXT");
         });
    });
}

function DeleteBoard(BoardId)
{
    var html = "<div>您确定要删除此板块吗</div>";
    
    OpenDialogBoxByText(html,"删除板块",350,function(){
        $("#idBoxConfirm").bind("click",function(){
            CloseBox();
            $.post("/SecuritiesInfo/ajax_DeleteBoard/",{BoardId:BoardId},function(data){
          
                if(data == "success")
                {
                    window.location.reload();
                }
                else OpenAlertBoxByText("删除失败","数据错误",350,null);
            },"TEXT");
         });
    });
}

function showPosition()
{
    $("#deal").hide();    
    $("#position").show();    
}

function showDeal()
{
    $("#position").hide();   
    $("#deal").show();    
}

function showPopularity()
{
    $("#latest").hide();    
    $("#popularity").show();   
}

function showLatest()
{ 
    $("#popularity").hide();   
    $("#latest").show();   
}

function changeColor()
{  
    var price = $("#price").val();
    $(".data").each(
        function(i,n)
        {
            if($(n).html() > price)
                $(n).css("color","red");
            else if ($(n).html() < price)
                $(n).css("color","green");
        }
    );
}

var url = "";
function getMarketView(code)
{
    var key = Number($("#techType").val());
    var k = Math.random();
    //分时图
    if(key==1)
    {
        url = "http://image2.sinajs.cn/newchart/min/n/"+code+".gif?";
        $("#quote").attr("src",url+k);
        $("#quote").attr("title","分时图");
    }
    //半年K线
    if(key==2)
    {
        url = "http://image2.sinajs.cn/newchart/daily_2/n/"+code+".gif?"; 
        $("#quote").attr("src",url+k);
        $("#quote").attr("title","半年K线");
    }
    //一年K线
    if(key==3)
     {
        url = "http://image2.sinajs.cn/newchart/daily_3/n/"+code+".gif?";
        $("#quote").attr("src",url+k);
        $("#quote").attr("title","一年K线");
    }
    //周K线
    if(key==4)
    {
        url = "http://image2.sinajs.cn/newchart/weekly/n/"+code+".gif?";
        $("#quote").attr("src",url+k);
        $("#quote").attr("title","周K线");
    }
}

var interval;
function checkRefresh(id,code)
{
    if($("#autoRefresh").attr("checked") == true)
    {
        interval = setInterval(
            function()
            {
                refresh(id,code);
            },30000);
    }
    else
        clearInterval(interval);
}

function refresh(id,code)
{
    if(url.length == 0)
        url = "http://image2.sinajs.cn/newchart/min/n/"+code+".gif?";
    var k = Math.random();
    $("#quote").attr("src",url+k);
    
    $.getJSON("/Securities/Ajax_GetSecurityData",{sid:id},
        function(jData)
        {  
            $("#priceNow").html(jData.Security[0].priceNow);
            $("#range").html(jData.Security[0].rangeNow);
            $("#maxPrice").html(jData.Security[0].highPrice);
            $("#minPrice").html(jData.Security[0].lowPrice);
            changeColor();
        });
}

function showAnalysisTable(analysis)
{
    var table = document.getElementsByTagName("tbody")[0];
    //清除表格数据
    while(table.getElementsByTagName("tr").length > 1)
    {
        table.removeChild(table.getElementsByTagName("tr")[1]);
    }
    
    var tr,td,textnode,link;
    
    if (analysis.length > 0)
    {
      for(var i = 0; i < analysis.length; i++)
      {
        tr = document.createElement("tr");
        
        td = document.createElement("td");
        textnode = document.createTextNode(analysis[i].Type);
        td.appendChild(textnode);
        tr.appendChild(td);
        
        td = document.createElement("td");
        link = document.createElement("a");
        link.setAttribute("href","/Analysis/ViewAnalysisInfo?aid="+analysis[i].AnalysisId);
        link.setAttribute("title",analysis[i].Title);
        link.innerHTML = analysis[i].TitleAbstract;
        td.appendChild(link);
        tr.appendChild(td);
        
        td = document.createElement("td");
        link = document.createElement("a");
        link.setAttribute("href","/Analysis/FindUserAnalysis?uid="+analysis[i].UserId);
        link.innerHTML = analysis[i].Author;
        td.appendChild(link);
        tr.appendChild(td);
        
        td = document.createElement("td");
        textnode = document.createTextNode(analysis[i].FocusCount);
        td.appendChild(textnode);
        tr.appendChild(td);
        
        td = document.createElement("td");
        textnode = document.createTextNode(analysis[i].RecommendCount);
        td.appendChild(textnode);
        tr.appendChild(td);
        
        td = document.createElement("td");
        textnode = document.createTextNode(analysis[i].ViewCount);
        td.appendChild(textnode);
        tr.appendChild(td);
        
        td = document.createElement("td");
        textnode = document.createTextNode(analysis[i].CreateTime);
        td.appendChild(textnode);
        tr.appendChild(td);
        
        table.appendChild(tr);
      }
    }
    else
    {
        tr = document.createElement("tr");
        
        td = document.createElement("td");
        td.setAttribute("colSpan","7");
        textnode = document.createTextNode("没有相关点评");
        td.appendChild(textnode);
        tr.appendChild(td);
        
        table.appendChild(tr);
    }
}

function changeAnalysis(link,action, code)
{
    $(".subnav a.on").removeClass("on");
    link.className = "on";
    var t = Math.random();
    showLoadingAnalysis();
    $.getJSON("/SecuritiesInfo/ajax_changeAnalysis",{SID:code,action:action,t:t},function(json){
        var analysis = json["Info"].Analysis;
        showAnalysisTable(analysis);
    });
}

function showLoadingAnalysis()
{
    var table = document.getElementsByTagName("tbody")[0];
    //清除表格数据
    while(table.getElementsByTagName("tr").length > 1)
    {
        table.removeChild(table.getElementsByTagName("tr")[1]);
    }
    var tr,td;
    tr = document.createElement("tr");
    td = document.createElement("td");
    td.setAttribute("colSpan","7");
    textnode = document.createTextNode("点评数据加载中...");
    td.appendChild(textnode);
    tr.appendChild(td);
    table.appendChild(tr);
}


//获取证券最新价格
//输入， id:证券的ID，例如：1001|2001|2
//输出，json，JSON格式的证券最新价,获取价格例子为json["Data"][1].Price,获取涨跌幅:json["Data"][1].UpDownRate
function getNewPrice(id,time)
{
    setInterval(function(){
        $.getJSON("/securitiesinfo/ajax_getNewFocusPrice",{id:id,t:Math.random()},function(json){
            return json;
        });
    },time);
}

